package com.res.simulator.transformer;

import java.util.HashSet;
import java.util.Set;

import com.res.common.transformer.BaseTransformer;
import com.res.simulator.model.SavedAPIModel;
import com.res.simulator.view.ConsumerTemplateSaveView;
import com.sip.dbadapter.dao.domain.ApiDefinition;
import com.sip.dbadapter.dao.domain.ConsumeTemplate;
import com.sip.dbadapter.dao.domain.ScanTypeRest;

public class ConsumeTemplateSaveTransformer implements BaseTransformer<SavedAPIModel, ConsumerTemplateSaveView, ApiDefinition> {

    public ApiDefinition transformModelToDomian(final SavedAPIModel model) throws Exception {

        final ApiDefinition apiDef = new ApiDefinition();

        apiDef.setApiId(model.getApiId());

        final Set<ConsumeTemplate> consumeTemplateSet = new HashSet<ConsumeTemplate>();
        final ConsumeTemplate consumeTemplate = new ConsumeTemplate();

        consumeTemplate.setContent(model.getApiString());// Consumer Template text
        consumeTemplate.setTemplateId(model.getTemplateId());

        consumeTemplateSet.add(consumeTemplate);

        ScanTypeRest rest = new ScanTypeRest();

        rest.setConsumeTemplateSet(consumeTemplateSet);

        apiDef.setScanType(1);// Currently REST ScanType is hardCoded
        apiDef.setScanTypeRest(rest);// New Consumer Template Data is set into rest Instance. TODO- ScanType
                                     // (REST/RMI/WS) should be select based on Type comes from front end.

        return apiDef;
    }

    public ConsumerTemplateSaveView transformDomianToView(final ApiDefinition domain) throws Exception {

        final ConsumerTemplateSaveView saveApiView = new ConsumerTemplateSaveView();
        
        saveApiView.setApiId(String.valueOf(domain.getApiId()));
        saveApiView.setApiName(domain.getName());

        return saveApiView;
    }

}
